package com.ipay.wechat.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ipay.utils.Constant;
import com.ipay.utils.DateUtils;
import com.ipay.utils.RandomStringUtils;
import com.ipay.wechat.pojo.BankInfo;
import com.ipay.wechat.pojo.QRCode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import com.ipay.wechat.dao.BankInfoDao;
import com.ipay.wechat.dao.CashiersDao;
import com.ipay.wechat.dao.QRCodeDao;
import com.ipay.wechat.pojo.Cashiers;
import com.ipay.wechat.service.CashiersService;

@Transactional
@Service("cashiersService")
public class CashiersServiceImpl implements CashiersService {
	@Autowired
	private CashiersDao cashiersDao;
	@Autowired
	private QRCodeDao qRCodeDao;
	@Autowired
	private BankInfoDao bankInfoDao;

	@Override
	public Cashiers findCashierByMobile(String mobile,int activateState) {
		return cashiersDao.findCashierByMobile(mobile,activateState);
	}
	
	@Override
	public void insertCashier(String name, String mobile, int activateState,
			String merchantId, int authority, int role,String inviteCode) {
		Cashiers cashiers=new Cashiers();
		cashiers.setAuthority(authority);
		cashiers.setMerchantId(merchantId);
		cashiers.setMobilephone(mobile);
		cashiers.setName(name);
		cashiers.setRole(role);
		cashiers.setActivateState(activateState);
		cashiers.setInviteCode(inviteCode);
		cashiersDao.insertCashier(cashiers);
		
		String date = DateUtils.format(new Date(), "yyyyMMdd");
        String random = RandomStringUtils.randomNumeric(6);
        String codeNo = date + random;
		
        StringBuffer qrurl = new StringBuffer();
		//下载时访问地址
		qrurl.append("merchant/userAgent");//http://www.***.cn/wechat/merchant/userAgent   Constant.DOMAINURL+
		qrurl.append("?merchantNo=" + merchantId);// 商户号
        
		//新增二维码
		QRCode qRCode = new QRCode();
		qRCode.setCodeName("主码");        //收款码名称
		qRCode.setCodeNo(codeNo);       //收款码编号
		qRCode.setMerchantId(merchantId); //商户编号
		qRCode.setUserId(cashiers.getId());
		qRCode.setUrl(qrurl.toString());         //访问路径
		qRCode.setDeleteStatus(String.valueOf(Constant.DELETE_STATUS_NO));	//删除状态  0：未删除1：已删除 
		qRCode.setCodeStatus(Constant.CODE_STATUS_SD);		//二维码状态  0、系统生成 1、手动添加
		
		//根据商户号查询bankinfo表 主键id 与 默认二维码绑定
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("merchantNo", merchantId);
		List<BankInfo> bankList = bankInfoDao.queryList(map);
		if(bankList.size()>0){
			qRCode.setBankId(bankList.get(0).getId());//银行信息表主键id
		}else{
			qRCode.setBankId(Long.parseLong("0"));//默认值
		}
		qRCodeDao.insertQrCode(qRCode);
	}

	@Override
	public List<Cashiers> findCashierList(String merchantNo, Long mId) {
		// TODO Auto-generated method stub
		Cashiers cashiers=new Cashiers();
		cashiers.setMerchantId(merchantNo);//商户编号
		cashiers.setMid(mId);//商户id
		return cashiersDao.findCashierList(cashiers);
	}
	@Override
	public Cashiers findCashierByMobileAndState(String mobilephone, int activateState) {
		return cashiersDao.findCashierByMobileAndState(mobilephone,activateState);
	}


	@Override
	public Cashiers findCashierByOpenid(String openid, int activateState) {
		return cashiersDao.findCashierByOpenid(openid,activateState);
	}

	@Override
	public void updateCashier(String mobile,String merchantId,int activateState) {
		// TODO Auto-generated method stub
		Cashiers cashiers=new Cashiers();
		cashiers.setMobilephone(mobile);
		cashiers.setActivateState(activateState);
		cashiers.setMerchantId(merchantId);
		cashiersDao.updateCashier(cashiers);
	}

	@Override
	public int delCashier(String mobile,@RequestParam String merchant_id) {
		// TODO Auto-generated method stub
		return cashiersDao.delCashier(mobile,merchant_id);
	}

	@Override
	public int modifyCashier(String name,String mobilephone) {
		// TODO Auto-generated method stub
		Cashiers cashiers=new Cashiers();
		cashiers.setName(name);
		cashiers.setMobilephone(mobilephone);
		return cashiersDao.updateCashier(cashiers);
	}

	@Override
	public void update(Cashiers cashiers) {
		cashiersDao.update(cashiers);
	}
	
	@Override
	public int updateCashierState(String activateState,String mobile) {
		// TODO Auto-generated method stub
		return cashiersDao.updateCashierState(activateState,mobile);
	}
	@Override
	public int updateCashierByMobile(String activateState,String mobile) {
		return cashiersDao.updateCashierByMobile(activateState, mobile);
	}

	@Override
	public boolean updateIsNotify(Long id, int isNotify) {
		if(cashiersDao.updateIsNotify(id, isNotify) > 0){
			return true;
		}
		return false;
	}

	@Override
	public List<Cashiers> findCashierByMerchantId(String merchantId) {
		return cashiersDao.findCashierByMerchantId(merchantId);
	}

	@Override
	public Cashiers findCashierById(Long id) {
		return cashiersDao.findCashierById(id);
	}

	@Override
	public List<Cashiers> findListByMerchantId(String merchantId) {
		//根据商户号查询收款员表 返回list集合
		List<Cashiers> list = cashiersDao.findCashierByMerchantId(merchantId);
		if(list.size()>0){
			//遍历集合，根据userId查询二维码表 返回集合
			for(Cashiers cashiers:list){
				QRCode code = new QRCode();
				code.setUserId(cashiers.getId());
				code.setMerchantId(cashiers.getMerchantId());
				
				List<QRCode>  codeList = qRCodeDao.findQRCodeByUserId(code);
				if(codeList.size() > 0){
					cashiers.setCodeList(codeList);
				}
			}
		}
		return list;
	}

}
